package com.hd.trans.flagremote

import android.annotation.SuppressLint
import android.view.ViewGroup
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.hd.trans.R

@SuppressLint("StaticFieldLeak")
object FlagRemoteCore {

    @SuppressLint("UseCompatLoadingForDrawables")
    fun loadFlagDrawable(imageView: ImageView, code: String?, size: SizeRemote = SizeRemote.W169) {
        val name = when(size){
            SizeRemote.W57-> "flag1x/${code}_flag.png"
            SizeRemote.W113-> "flag2x/${code}_flag.png"
            SizeRemote.W225-> "flag4x/${code}_flag.png"
            else -> "flag3x/${code}_flag.png"
        }
        val defaultRes = when(size){
            SizeRemote.W57-> imageView.context.resources.getDrawable(R.drawable.default_1x_flag, null)
            SizeRemote.W113-> imageView.context.resources.getDrawable(R.drawable.default_2x_flag, null)
            SizeRemote.W225-> imageView.context.resources.getDrawable(R.drawable.default_4x_flag, null)
            else -> imageView.context.resources.getDrawable(R.drawable.default_3x_flag, null)
        }
        val url = "https://oss-liuchengtu.hudunsoft.com/android_cloud_json/com.nnxieli.flagprovider/$name"

        if(imageView.layoutParams.width == ViewGroup.LayoutParams.WRAP_CONTENT
            && imageView.layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT){
            Glide.with(imageView)
                .load(url)
                .error(defaultRes)
                .override(size.w, size.h)
                .into(imageView)
        }else{
            //加载原图
            Glide.with(imageView)
                .load(url)
                .error(defaultRes)
                .into(imageView)
        }
    }
}